package com.firebird.app.kotlin.stock.trend.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import android.widget.HorizontalScrollView
import android.widget.ScrollView
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.R
import java.util.*


/**
 * 股票分时走势窗口中，股票文本资讯的Tab滑动控件
 * @author ljk
 * @date   2019年11月4日
 */
class StockTextDataTabView : HorizontalScrollView, View.OnClickListener {
    private var parentScrollView: ScrollView? = null
    private var mContext: Context? = null
    private var tv_news: TextView? = null
    private var tv_pankou: TextView? = null
    private var tv_lungu: TextView? = null
    private var tv_gonggao: TextView? = null
    private var tv_summary: TextView? = null
    private var tv_caiwu: TextView? = null
    private var tv_yanbao: TextView? = null
    private var tvs: ArrayList<TextView?>? = null
    private var screenWidth //屏幕像素宽度
            = 0
    private var tabNum //菜单的个数
            = 0
    private var currentTab = 0
    private var currentTabBackgroundColor = 0

    constructor(context: Context?) : super(context, null) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.animationTabWidget)
        currentTab = typedArray.getInt(R.styleable.animationTabWidget_current_index, 0)
        tabNum = typedArray.getInt(R.styleable.animationTabWidget_visable_tab_num, 1)
        typedArray.recycle()
        currentTabBackgroundColor = context.resources.getColor(R.color.menu_gray_common)
        View.inflate(context, R.layout.layout_stock_textdata_tabview, this)
        tvs = ArrayList()
        tv_news = findViewById<View>(R.id.tv_news) as TextView
        tv_pankou = findViewById<View>(R.id.tv_pankou) as TextView
        tv_lungu = findViewById<View>(R.id.tv_lungu) as TextView
        tv_gonggao = findViewById<View>(R.id.tv_gonggao) as TextView
        tv_summary = findViewById<View>(R.id.tv_summary) as TextView
        tv_caiwu = findViewById<View>(R.id.tv_caiwu) as TextView
        tv_yanbao = findViewById<View>(R.id.tv_yanbao) as TextView
        tvs!!.add(tv_news)
        tvs!!.add(tv_pankou)
        tvs!!.add(tv_lungu)
        tvs!!.add(tv_gonggao)
        tvs!!.add(tv_summary)
        tvs!!.add(tv_caiwu)
        tvs!!.add(tv_yanbao)
        setTvWidth(tabNum)
        setCurrentcurrentTab(currentTab)
    }

    /**
     * 设置组合控件中每个 TextView的宽度
     * @param num    当前屏幕宽度中显示TextView的个数
     */
    private fun setTvWidth(num: Int) {
        val dm = DisplayMetrics()
        val wm = context!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wm.defaultDisplay.getMetrics(dm)
        screenWidth = dm.widthPixels // 获取屏幕分辨率宽度
        val tvWidth = screenWidth / num
        for (tv in tvs!!) {
            tv!!.width = tvWidth
        }
    }

    fun setParentScrollView(scrollView: ScrollView?) {
        parentScrollView = scrollView
    }

    /**
     * 为每项菜单注册点击事件
     * @param listener
     */
    override fun setOnClickListener(listener: OnClickListener) {
        for (tv in tvs!!) {
            tv!!.setOnClickListener(listener)
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_news -> currentTab = 0
            R.id.tv_pankou -> currentTab = 1
            R.id.tv_lungu -> currentTab = 2
            R.id.tv_gonggao -> currentTab = 3
            R.id.tv_summary -> currentTab = 4
            R.id.tv_caiwu -> currentTab = 5
            R.id.tv_yanbao -> currentTab = 6
        }
        if (parentScrollView != null) {
            if (parentScrollView!!.scrollY == 0) {
                parentScrollView!!.scrollTo(0, 60)
            }
        }
        setCurrentcurrentTab(currentTab)
    }

    /**
     * 当前菜单项不在屏幕中央时，滚动scroolview
     * @param currentTab
     */
    private fun setCurrentcurrentTab(currentTab: Int) {
        for (tv in tvs!!) {
            tv!!.setBackgroundColor(Color.WHITE)
        }
        val tv = tvs!![currentTab]
        tv!!.setBackgroundColor(currentTabBackgroundColor)
        val tvWidth = tv.width
        val textViewX = currentTab * tvWidth
        if (textViewX > tvWidth * 2) {
            scrollTo((currentTab - 2) * tvWidth, 0)
        } else if (textViewX < tvWidth * 2) {
            scrollTo(-tvWidth, 0)
        }
    }
}